﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageAdmin
{
    public partial class PageEditProfile : System.Web.UI.Page
    {
        private CNhanVien _cnv = new CNhanVien();
        private bool _flag = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            pnThongBao.Visible = false;
            lbThongBao.Text = "";
            if (!IsPostBack)
            {
                LoadInfoEmployee();
            }
        }

        private void LoadInfoEmployee()
        {
            int id = Convert.ToInt32(Session["IDDN"].ToString());
            NhanVien nv = _cnv.GetAccountID(id);
            txtAddress.Text = nv.DiaChi;
            txtEmail.Text = nv.Email;
            txtFirstname.Text = nv.HoTen;
            if (nv.LoaiND == 3)
            {
                lbType.Text = "Employee management";
            }
            else if (nv.LoaiND == 4)
            {
                lbType.Text = "HR management";
            }

                rblGender.Items.FindByValue(nv.GioiTinh.Value.ToString().ToLower()).Selected = true;
        }

        protected void btnJoin_Click(object sender, EventArgs e)
        {
            if (_flag)
            {
                NhanVien nv = new NhanVien();
                nv.IDTaiKhoan = Convert.ToInt32(Session["IDDN"].ToString());
                nv.Email = txtEmail.Text;
                if (rblGender.SelectedValue != "-1")
                {
                    nv.GioiTinh = Convert.ToBoolean(rblGender.SelectedValue);
                }
                nv.HoTen = txtFirstname.Text;
                if (!txtAddress.Text.Equals(""))
                {
                    nv.DiaChi = txtAddress.Text;
                }
                bool kq = _cnv.UpdateStaff(nv);
                if (kq)
                {
                    LoadInfoEmployee();
                    pnThongBao.Visible = true;
                    pnThongBao.CssClass = "InLeft success message";
                    lbThongBao.Text = "Update successfully.";
                }
                else
                {
                    pnThongBao.Visible = true;
                    pnThongBao.CssClass = "InLeft error message";
                    lbThongBao.Text = "Update failure.";
                }

            }
        }

        protected void btnEmail_Click(object sender, EventArgs e)
        {
            txtEmail.Enabled = true;
        }

        protected void cusvEmail_ServerValidate(object source, ServerValidateEventArgs args)
        {
            
            if (Session["IDDN"].ToString().Equals(""))
            {
                int id = Convert.ToInt32(Session["IDDN"].ToString());
                string email = txtEmail.Text;
                bool f = _cnv.EmailExistTestID(email,id);
                if (f)
                {
                    args.IsValid = true;
                }
                else
                {
                    _flag = false;
                    args.IsValid = false;
                }
            }
        }
    }
}